package com.example.deliveryapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class ChikenWingsActivity extends AppCompatActivity implements CustomAdapt {
    private EditText editText;
    private Button add_chikenwings,minus_chickenwings;
    private TextView textViewPrice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        // 设置内容视图为您的布局XML
        setContentView(R.layout.chicken_wings);

        Button vieworder_chickenwings = findViewById(R.id.vieworder_chickenwings);
        vieworder_chickenwings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ChikenWingsActivity.this,ViewOrderPageActivity.class);
                startActivity(intent);
            }
        });
        editText = findViewById(R.id.edit_chickenwings);
        add_chikenwings = findViewById(R.id.add_chickenwings);
        minus_chickenwings = findViewById(R.id.minus_chickenwings);

        add_chikenwings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentValue = Integer.parseInt(editText.getText().toString());
                if (currentValue < 99){
                    editText.setText(String.valueOf(++currentValue));
                }
            }
        });
        minus_chickenwings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentValue = Integer.parseInt(editText.getText().toString());
                if (currentValue >0) {
                    editText.setText(String.valueOf(--currentValue));
                }
            }
        });
        editText= findViewById(R.id.edit_chickenwings);
        textViewPrice = findViewById(R.id.price_chickenwings);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //数量变化之前
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //数量变化时
            }

            @Override
            public void afterTextChanged(Editable s) {
                //数量变化之后更新价格
                updatePrice();
            }
        });
        updatePrice();
    }
    private void updatePrice(){
        final double unitPrcie = 9.35;
        int quantity = getSafeInt(editText);
        double totalPrice = quantity*unitPrcie;
        String formattedPrice = String.format(Locale.getDefault(),"Price:%.2f",totalPrice);
        textViewPrice.setText(formattedPrice);
    }
    private int getSafeInt(EditText editText){
        try {
            return Integer.parseInt(editText.getText().toString());
        }catch (NumberFormatException exception){
            return 0;
        }
    }
}
